package org.feifei.hrm.controller;

import org.feifei.hrm.dto.CourseDto;
import org.feifei.hrm.service.ICourseService;
import org.feifei.hrm.domain.Course;
import org.feifei.hrm.query.CourseQuery;
import org.feifei.hrm.util.AjaxResult;
import org.feifei.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;


    /**
     * 保存和修改公用的
     * @param courseDto  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody @Valid CourseDto courseDto){
        Course course = courseDto.getCourse();
        if( course.getId()!=null)
                courseService.updateById(courseDto);
            else
                courseService.insert(courseDto);
            return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize("isAnonymous()")
    public PageList<Course> json(@RequestBody CourseQuery query)
    {

        return courseService.selectPageList(query);
    }


    @PostMapping("/online") //上线
    public AjaxResult online(@RequestBody List<Long> ids)
    {
        return courseService.online(ids);
    }

    //下线
    @PostMapping("/offline")
    public AjaxResult offline(@RequestBody List<Long> ids)
    {
        return courseService.offline(ids);
    }


}
